<?php
class Product_groups_model extends CI_Model {

    var $id = '';
    var $description = '';
    private $_tabla = 'product_groups';

    function __construct() {
        // Call the Model constructor
        parent::__construct();
    }

    public function find($desc)
    {
        $query = $this->db->where('description', $desc)->get($this->_tabla);
        $rs = $query->result();
        if (isset($rs[0])) {
            return $rs[0];
        } 
        return false;
    }

    public function findCode($code)
    {
        $query = $this->db->where('id', $code)->get($this->_tabla);
        $rs = $query->result();
        if (isset($rs[0])) {
            return $rs[0];
        } 
        return false;        
    }
    
    public function getAll()
    {
        $query = $this->db;
        $query = $query->where('status', 1);
        $query = $this->db->get($this->_tabla);
        return $query->result();
    }

    public function add($data)
    {
        $this->db->insert($this->_tabla, $data);
        return true;
    }

    public function search($code = null, $description = null)
    {
        $query = $this->db;
        if (isset($code) && $code != '') {
            $query = $query->like('id', $code, 'both');
        }
        if (isset($description) && $description != '') {
            $query = $query->like('description', $description, 'both');
        }
        $query = $query->where('status', 1);
        $query = $query->get($this->_tabla);
        $rs = $query->result();
        return $rs;
    }

    public function update($code, $data)
    {
        $this->db->where('id', $code);
        $this->db->update($this->_tabla, $data);
        return true;
    }

    public function delete($code)
    {
        $this->db->where('id', $code);        
        $this->db->update($this->_tabla, array('status' => 0));
        return true; 
    }
}